/**
 * Riceve il messaggio dal broadcast e lo reinvia nel postmessage.
 */
var finestre=new Array();
var myWindow=new Array();
onconnect = function(e) {
	var port = e.ports[0];
	finestre.push(e.ports[0]);
	port.onmessage = function(e) {
		
		if(e.data.func != undefined && e.data.func == "register"){
			var found = false;
			for(i in myWindow){
				if(myWindow[i] == e.data.nome){
					found = true;
				}
			}
			if(!found){
				myWindow.push(e.data.nome);
				port.postMessage(" REGISTRATI: "+myWindow);
			}
		}
		else if(e.data.func != undefined && e.data.func == "controllo"){
			var found = false;
			var obj={};
			obj.func = "controllo";
			obj.nome = e.data.nome;
			for(i in myWindow){
				if(myWindow[i] == e.data.nome){
					found=true;
				}
			}
			obj.stato = found;

			port.postMessage(obj);
		}
		else if(e.data.func != undefined && e.data.func == "remove"){
			for(i in myWindow){
				if(myWindow[i] == e.data.nome && trovato == false){
					myWindow.splice(i, 1);
					trovato = true;
				}
			}
		}
		else{
		   var workerResult = e.data;
		   var tempport;
		   for (index = 0; index < finestre.length; index++) {
			   tempport = finestre[index];
			   try{
				   tempport.postMessage(workerResult);
			   }catch(e){
				   port.postMessage(e + ' - error - ' + index);
			   }
			} 
		}
	}
};